package tgc.edu.yzy.web.admin.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.Custom;
import tgc.edu.yzy.custom.DatagridUtils;
import tgc.edu.yzy.entity.BmsRole;
import tgc.edu.yzy.entity.Book;
import tgc.edu.yzy.entity.BookRecord;
import tgc.edu.yzy.entity.Record;
import tgc.edu.yzy.entity.User;
import tgc.edu.yzy.service.BookRecordService;
import tgc.edu.yzy.service.BookService;
import tgc.edu.yzy.service.RecordService;
import tgc.edu.yzy.service.UserService;

@Controller
@RequestMapping(value="admin/personal")
public class Personal {
	@Autowired
	private BookRecordService brservice;
	@Autowired
	private UserService userservice;
	@Autowired
	private RecordService recordservice;
	@Autowired
	private Custom custom;
	@Autowired
	private BookService bookservice;
	/**
	 * 跳转到还书页面
	 * @param map
	 * @return
	 */
	@RequestMapping(value="/ReturnBooks")
	private String ReturnBooks(ModelMap map) {
		List<BookRecord> books=brservice.findByUserAndState(userservice.findById(custom.userid()),"同意借书");
		map.put("books", books);
		return "admin/personal/ReturnBooks";
	}
	/***
	 * 归还图书
	 * 同时需要对多张表进行处理
	 * @param id
	 * @return
	 */
	@RequestMapping(value="Return")
	@ResponseBody
	private String Return(Integer id) {
		
		
		//对借书记录表
		BookRecord brs=brservice.findById(id);
		User users = brs.getUser();
		List<BmsRole> bmsRole = users.getBmsRole();
		String code=null;
		for (BmsRole role : bmsRole) {
			code=role.getCode();
		}
		if("ROLE_USER".equals(code)) {
			brs.setReturnTime(custom.time());;
			brs.setState("还书等待处理");
			brservice.save(brs);
			
			//加一条记录
			custom.booksrecord(brs.getUser(), brs.getBook(), "还书声明", brs.getUser().getName()+"提交归还图书声明");
		}else {
			brservice.delete(brs);
			
			custom.booksrecord(brs.getUser(),brs.getBook(), "确认还书", "图书管理员已还书");
			Integer number = users.getNumber();
			number=number-1;
			users.setNumber(number);
			userservice.save(users);
			
			Book books = brs.getBook();
			Integer loan = books.getLoan()+1;
			Integer state = books.getState()+1;
			books.setState(state);
			books.setLoan(loan);
			bookservice.save(books);
		}
		return "";
		
	}
	/**
	 * 个人记录查询
	 * @return
	 */
	@RequestMapping(value="/record")
	private String record() {
		return "admin/personal/onerecord";
	}
	
	@RequestMapping(value="pagelist")
	@ResponseBody
	private Object pagelist(DatagridUtils form,Record record,String bookname) {
		User users=userservice.findById(custom.userid());
		Pageable pageable=form.buildPageable();
		Page<Record> books=null;
		Specification<Record> spec = new Specification<Record>() {

			@Override
			public Predicate toPredicate(Root<Record> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(record.getName())) {
					rules.add(criteriaBuilder.like(root.get("name"), "%"+record.getName()+"%"));
				}else if(StringUtils.hasText(record.getTime())) {
					rules.add(criteriaBuilder.like(root.get("time"), record.getTime()+"%"));
				}else if(StringUtils.hasText(record.getContent())) {
					rules.add(criteriaBuilder.like(root.get("content"), "%"+record.getContent()+"%"));
				}else if(StringUtils.hasText(bookname)) {
					rules.add(criteriaBuilder.like(root.get("book").get("name"), "%"+bookname+"%"));
				}
				rules.add(criteriaBuilder.equal(root.get("user").get("id"), users.getId()));
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		books= recordservice.findAll(spec,pageable);
		HashMap<String , Object> result = form.getPageResult(books);
		return result; 
	}
}
